/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using MyPhotoIndex.Controls;
using System.Drawing;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces.Imaging;
using MyPhotoIndex.ImageBank;

namespace MyPhotoIndex
{
    [Serializable]    
    public class MPICategoryRenderer : CategoryRenderer
    {
        [NonSerialized]
        private ImageList m_imageList;
        [NonSerialized]
        private static Image ms_emptyCategoryImage = Properties.Resources.Category00;

        public MPICategoryRenderer(ImageList imageList)
        {
            m_imageList = imageList;
        }

        public override void Render(Graphics graphics, Rectangle drawingRect, MyPhotoIndex.Interfaces.Imaging.IImageData imageData)
        {
            int numOfCategories = 0;
            foreach (int categoryId in imageData.Categories)
            {
                if (categoryId > 0)
                {
                    numOfCategories++;
                }
            }

            if (numOfCategories == 0)
            {
                graphics.DrawImage(ms_emptyCategoryImage, drawingRect);
  
                return;
            }

            IImageCategoryCollection categories = ImageCategoryCollection.Instance;

            if (numOfCategories == 1)
            {
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[0]).ColorIndex], drawingRect);

                return;
            }
            else
            if (numOfCategories == 2)
            {
                Rectangle firstRect = drawingRect;
                firstRect.Width /= 2;
                Rectangle secondRect = firstRect;
                secondRect.X = firstRect.X + firstRect.Width + 1;

                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[0]).ColorIndex], firstRect);
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[1]).ColorIndex], secondRect);

                return;
            }
            else
            if (numOfCategories == 3)
            {
                Rectangle firstRect = drawingRect;
                firstRect.Width /= 2;
                firstRect.Height /= 2;

                Rectangle secondRect = firstRect;
                secondRect.X = firstRect.X + firstRect.Width + 1;

                Rectangle thirdRect = firstRect;
                thirdRect.Y = firstRect.Y + firstRect.Height + 1;

                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[0]).ColorIndex], firstRect);
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[1]).ColorIndex], secondRect);
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[2]).ColorIndex], thirdRect);

                return;
            }
            else
            if (numOfCategories == 4)
            {
                Rectangle firstRect = drawingRect;
                firstRect.Width /= 2;
                firstRect.Height /= 2;

                Rectangle secondRect = firstRect;
                secondRect.X = firstRect.X + firstRect.Width + 1;

                Rectangle thirdRect = firstRect;
                thirdRect.Y = firstRect.Y + firstRect.Height + 1;

                Rectangle forthRect = secondRect;
                forthRect.Y = secondRect.Y + secondRect.Height + 1;

                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[0]).ColorIndex], firstRect);
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[1]).ColorIndex], secondRect);
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[2]).ColorIndex], thirdRect);
                graphics.DrawImage(m_imageList.Images[categories.GetById(imageData.Categories[3]).ColorIndex], forthRect);

                return;
            }
        }
    }
}
